/**
 * 
 */
package com.dudu.controller;

import java.util.Calendar;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.mortbay.log.Log;

import com.dudu.bean.AccountBean;
import com.dudu.enumeration.OperateTypeEnum;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.IAccountManageService;
import com.dudu.service.ICheckValidityService;
import com.dudu.service.ILogService;
import com.dudu.service.impl.AccountExtendServiceImpl;
import com.dudu.service.impl.AccountManageServiceImpl;
import com.dudu.service.impl.CheckValidityServiceImpl;
import com.dudu.service.impl.LogService;
import com.dudu.utils.DateUtils;
import com.dudu.utils.DictionaryUtils;
import com.dudu.utils.EnumUtility;
import com.dudu.utils.MD5Utils;

import core.CoreService;

/**
 * 根据Account查询信息实现类
 * 
 * @author 唐建
 * @date 2016-7-12
 */
public class OpenOperateByAccountController extends CoreService implements IOpenOperateByAccountController {

	// 用于记录相应接口调用的日志
	private ILogService logService = new LogService();
	// 账号
	private IAccountExtendService accountExtendService = new AccountExtendServiceImpl();
	// 对外返回参数
	private JSONObject object = new JSONObject();
	// 3.0 地址
	private String url = DictionaryUtils.getValueByTypeAndKey("url", "dudu3.0");

	/**
	 * 根据Account查询信息
	 * 
	 * @param cust_account     计费账号/账号
	 * */
	@Override
	public String qryByAccount() {
		Log.info("load OpenOperateByAccount.qryByAccount 根据Account查询相关信息");
		double begin = Calendar.getInstance().getTimeInMillis();
		// 获取appkey参数
		String cust_account = this.getRequest().getParameter("cust_account");
		// 获取timestamp参数
		String timestamp = "";
		// 获取验证码参数
		String sign = "";
		try {
			accountExtendService.qryVersionFour(object, cust_account,timestamp, sign);
			if (!(object.get("result").equals("0"))) {
				if (object.get("code").equals("0002")) {
					// 查询v3版本是否存在
					object = accountExtendService.qryVersionThree(cust_account,url, timestamp, sign);
				}
			}
		} catch (Exception e) {
			log.info("********查询失败**********");
			object.put("result", "-1");
			object.put("describe", "错误");
		}
		// 拼凑字符串
		double end = Calendar.getInstance().getTimeInMillis();
		// 时间差
		double during = Double.valueOf((end - begin)) / 1000;
		String request_msg = "cust_account:" + cust_account + "|timestamp:"	+ timestamp + "|sign:" + sign;
		this.logService.logInterface(during, "qryByCustAccount", request_msg,object.toString(), object.getString("result"),
				DateUtils.getCurrentTime(), this.getRequest().getRemoteAddr(),null);
		log.info(request_msg);
		return returnData(object.toString());
	}

	/**
	 * 根据cust_account更新信息
	 * 
	 * @param cust_account 账号/计费账号
	 * @param timestamp    时间戳
	 * @param sign         验证码
	 * @param linker_phone 联系电话
	 * @param urlcallstatus会议推送状态地址
	 * @param urlticket    话单推送地址
	 * @param urlivrdigit  ivr按键结果推送地址
	 * @param urlrecord    录音文件推送地址
	 * */
	@Override
	public String updByAccount() {
		Log.info("load OpenOperateByApp.updByAccount 根据cust_account更新相关信息");
		double begin = Calendar.getInstance().getTimeInMillis();
		// 获取account参数
		String cust_account = this.getRequest().getParameter("cust_account");
		// 获取timestamp参数
		String timestamp = this.getRequest().getParameter("timestamp");
		// 获取验证码参数
		String sign = this.getRequest().getParameter("sign");
		// 获取会议状态推送地址
		String urlcallstatus = this.getRequest().getParameter("urlcallstatus");
		// 获取话单推送地址
		String urlticket = this.getRequest().getParameter("urlticket");
		// IVR按键结果推送地址
		String urlivrdigit = this.getRequest().getParameter("urlivrdigit");
		// 录音文件推送地址
		String urlrecord = this.getRequest().getParameter("urlrecord");
		// 录音文件推送地址
		String linker_phone = this.getRequest().getParameter("linker_phone");
		AccountBean qryResult = new AccountBean();
		try {
			qryResult = accountExtendService.qryVersionFour(object,cust_account, timestamp, sign);
			if (!(object.get("result").equals("0"))) {
				if (object.get("code").equals("0002")) {
					// 查询v3版本信息
					object = accountExtendService.qryVersionThree(cust_account,url, timestamp, sign);
					if (object.get("result").equals("0")) {
						object.put("privateKey", MD5Utils.getMd5Value());
						object.put("urlcallstatus", urlcallstatus);
						object.put("urlticket", urlticket);
						object.put("urlivrdigit", urlivrdigit);
						object.put("urlrecord", "");
						object.put("linker_phone", linker_phone);
						object = accountExtendService.updVersionThree(object,url, sign, timestamp, cust_account);
					}
				}
			}

			else {
				if (accountExtendService.checkSign(object, cust_account,timestamp, sign, qryResult)) {
					if (StringUtils.isNotBlank(urlcallstatus)) {
						qryResult.setUrl_callstatus(urlcallstatus);
					}
					if (StringUtils.isNotBlank(urlticket)) {
						qryResult.setUrl_ticket(urlticket);
					}
					if (StringUtils.isNotBlank(urlivrdigit)) {
						qryResult.setUrl_ivrdigit(urlivrdigit);
					}
					if (StringUtils.isNotBlank(linker_phone)) {
						qryResult.setLinker_phone(linker_phone);
					}
					qryResult.setKey_private(EnumUtility.trimNumm(MD5Utils.getMd5Value()));

					accountExtendService.updateAccountExtend(qryResult);
					// 设置更新值
					object.clear();
					object.put("result", 0);
					object.put("privateKey", qryResult.getKey_private());
					object.put("publicKey", qryResult.getKey_public());
					object.put("urlcallstatus", qryResult.getUrl_callstatus());
					object.put("urlticket", qryResult.getUrl_ticket());
					object.put("urlivrdigit", qryResult.getUrl_ivrdigit());
					object.put("urlrecord", qryResult.getUrl_record());
					object.put("linker_phone", qryResult.getLinker_phone());
					logService.log(OperateTypeEnum.EDIT.getKey(), 12, "公司运营","t_account_extend","对账号标识为:" + qryResult.getCust_account()	+ "通过acc更新应用部分信息");

				}
			}
		} catch (Exception e) {
			log.info("********更新失败**********");
			object.clear();
			object.put("result", "-1");
			object.put("describe", "错误");
		}
		// 拼凑字符串
		double end = Calendar.getInstance().getTimeInMillis();
		// 时间差
		double during = Double.valueOf((end - begin)) / 1000;
		// 拼日志内容
		String request_msg = "cust_account:" + cust_account+ "|timestamp:" + timestamp + "|sign:" + sign;
		this.logService.logInterface(during, "updByCustAccount", request_msg,object.toString(), object.getString("result"),
				DateUtils.getCurrentTime(), this.getRequest().getRemoteAddr(),null);
		return returnData(object.toString());
	}
}
